﻿
Partial Class WebForm_ApproveVolunteer_SearchNewVolunteer
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.InitData()
            RaiseEvent Loaded(Me, New EventArgs)
        End If
    End Sub

#Region "新註冊Gridview事件"

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            If Me.ddlStatus.SelectedValue = My.FormStatus.Approving Then
                TryCast(e.Row.FindControl("bfApplyDate"), Label).Text = "申請日期"
            ElseIf Me.ddlStatus.SelectedValue = My.FormStatus.Approved Then
                TryCast(e.Row.FindControl("bfApplyDate"), Label).Text = "通過日期"
            ElseIf Me.ddlStatus.SelectedValue = My.FormStatus.Rejected Then
                TryCast(e.Row.FindControl("bfApplyDate"), Label).Text = "退件日期"
            End If

        End If
    End Sub

    Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStatus.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlOrganization_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganization.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

#End Region

#Region "招募活動Gridview事件"

    Protected Sub gv2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv2.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            If Me.ddlStatus.SelectedValue = My.FormStatus.Approving Then
                TryCast(e.Row.FindControl("bfCreateDate"), Label).Text = "申請日期"
            ElseIf Me.ddlStatus.SelectedValue = My.FormStatus.Approved Then
                TryCast(e.Row.FindControl("bfCreateDate"), Label).Text = "通過日期"
            ElseIf Me.ddlStatus.SelectedValue = My.FormStatus.Rejected Then
                TryCast(e.Row.FindControl("bfCreateDate"), Label).Text = "退件日期"
            End If

        End If
    End Sub
    Protected Sub gv2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv2.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub gv2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv2.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv2_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv2.Sorting
        Me.BindGV()
    End Sub

#End Region

    Protected Sub ddlDataSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDataSource.SelectedIndexChanged
        If Me.ApplicationDataSource = My.VolunterApplicationDataSource.Enlistment Then
            Me.MultiView1.SetActiveView(Me.EnlistHeaderView)
            Me.MultiView2.SetActiveView(Me.EnlistHeaderGridView)
        ElseIf Me.ApplicationDataSource = My.VolunterApplicationDataSource.NewRegistration Then
            Me.MultiView1.SetActiveView(Me.OrganizationView)
            Me.MultiView2.SetActiveView(Me.OrganizationGridView)
        End If

        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ApproveVolunteer_SearchNewVolunteer
    Public Sub InitData()
        Me.BindDDLStatus()
        Me.BindDDLDataSource()
        Me.BindDDLOrganization()
        Me.BindDDLEnlistHeader()
    End Sub

    Private Sub BindDDLOrganization()
        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        Me.ddlOrganization.DataSource = MyObj.GetDDLOrganizationData()
        Me.ddlOrganization.DataBind()
    End Sub

    Private Sub BindDDLDataSource()
        Dim dt As Data.DataTable

        dt = MyObj.GetCodeMappingData("VolunteerApplication", "DataSource")

        Me.ddlDataSource.DataTextField = "Text"
        Me.ddlDataSource.DataValueField = "Value"
        Me.ddlDataSource.DataSource = dt
        Me.ddlDataSource.DataBind()
    End Sub

    Private Sub BindDDLEnlistHeader()
        Dim dat As New clsData
        'Dim sql As New MyStringBuilder
        Dim sql As New MyOrganizationDataFilter
        Dim dt As Data.DataTable

        sql.AppendFormat("select * from EnlistHeader eh ")
        'sql.AppendFormat("inner join UserOrganizationAuth ua on eh.UOrganizationID = dbo.GetUOrganizationID(ua.OrganizationID,ua.VOrganizationID) ")
        sql.AppendFormat("order by EnlistName")
        dt = dat.GetData(sql)

        Me.ddlEnlistHeader.DataTextField = "EnlistName"
        Me.ddlEnlistHeader.DataValueField = "EnlistID"
        Me.ddlEnlistHeader.DataSource = dt
        Me.ddlEnlistHeader.DataBind()

        'Me.ddlEnlistHeader.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindDDLStatus()
        Dim dt As Data.DataTable

        dt = MyObj.GetCodeMappingData("VolunteerApplication", "Status")

        Me.ddlStatus.DataTextField = "Text"
        Me.ddlStatus.DataValueField = "Value"
        Me.ddlStatus.DataSource = dt
        Me.ddlStatus.DataBind()
    End Sub

    Public Sub BindGV()
        If Me.ApplicationDataSource = My.VolunterApplicationDataSource.NewRegistration Then
            Me.MultiView2.SetActiveView(Me.OrganizationGridView)
            Me.BindGVForNewRegistration()
        ElseIf Me.ApplicationDataSource = My.VolunterApplicationDataSource.Enlistment Then
            Me.MultiView2.SetActiveView(Me.EnlistHeaderGridView)
            Me.BindGVForEnlistment()
        End If
    End Sub

    Public Sub BindGVForNewRegistration()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select distinct v.* ")
        sql.AppendFormat(",va.ApplicationID ")
        sql.AppendFormat(",r.TownName, r.RegionName ")
        'sql.AppendFormat(",dbo.fn_GetWishOrganization(v.VolunteerID,v.Active,va.Status,o.Root) as OrganizationName ")
        sql.AppendFormat(",dbo.fn_GetWishOrganization(v.VolunteerID,v.Active,va.Status) as OrganizationName ")
        If Me.ddlStatus.SelectedValue <> "WA" Then
            sql.AppendFormat(",u.UserName + ' ' + ov.OrganizationName as Approver ")
        Else
            sql.AppendFormat(",'' as Approver ")
        End If

        sql.AppendFormat(", case when va.Status = 'WA' then va.CreateDate else AR.ApproveDate end as ApplyDate ")
        sql.AppendFormat("from VolunteerDistinctView v ")
        sql.AppendFormat("inner join VolunteerApplication va on v.VolunteerID = va.VolunteerID ")
        sql.AppendFormat("inner join WishOrganization wo on v.VolunteerID = wo.VolunteerID ")
        sql.AppendFormat("inner join Village r on v.VillageID = r.RegionID ")
        sql.AppendFormat("inner join OrganizationTree o on wo.OrganizationID = o.OrganizationID ")

        'If Me.ddlStatus.SelectedValue <> "WA" Then
        sql.AppendFormat("left join ApproveRecord AR on va.ApplicationID = AR.FormID ")
        sql.AppendFormat("left join InternalUser u on ar.approver = u.userid ")
        sql.AppendFormat("left join OrganizationView ov on u.UOrganizationID = ov.UOrganizationID ")

        'End If
        sql.AppendFormat("where v.Active = '{0}' ", Me.VolunteerStatus)
        sql.AppendFormat("and va.Status = '{0}' ", Me.ddlStatus.SelectedValue)
        sql.AppendFormat("and o.URoot = '{0}' ", Me.ddlOrganization.SelectedValue)
        sql.AppendFormat("and wo.OrganizationID in( ")

        sql.AppendFormat("  select OrganizationID ")
        sql.AppendFormat("  from UserOrganizationAuth ")
        sql.AppendFormat("  where UserId = '{0}' ", clsAuth.GetUserID())
        sql.AppendFormat("  and OrganizationID <> -1 )")

        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()

        RaiseEvent DataBound(Me, New EventArgs)
        Me.gv.SelectedIndex = -1
    End Sub

    Public Sub BindGVForEnlistment()
        Me.SqlDataSource1.SelectParameters("EnlistID").DefaultValue = Me.ddlEnlistHeader.SelectedValue
        Me.gv2.DataBind()
        RaiseEvent DataBound(Me, New EventArgs)
        Me.gv2.SelectedIndex = -1

    End Sub

    Public WriteOnly Property SelectedIndex() As Integer
        Set(ByVal value As Integer)

            If Me.gv.Rows.Count > 0 Then
                Me.gv.SelectedIndex = value
                RaiseEvent SelectedIndexChanged(Me, New EventArgs)
            End If

        End Set
    End Property

    Public ReadOnly Property SelectedVolunteerID() As String
        Get
            If Me.gv.SelectedRow Is Nothing Then
                Return Me.gv.Rows(0).Cells(1).Text
            Else
                Return Me.gv.SelectedRow.Cells(1).Text
            End If

        End Get
    End Property

    Public ReadOnly Property SelectedGV2VolunteerID() As String
        Get
            If Me.gv2.SelectedRow Is Nothing Then
                Return Me.gv2.Rows(0).Cells(1).Text
            Else
                Return Me.gv2.SelectedRow.Cells(1).Text
            End If

        End Get
    End Property

    Public ReadOnly Property SelectedEnlistID() As String
        Get
            If Me.gv2.SelectedRow Is Nothing Then
                Return TryCast(Me.gv2.Rows(0).FindControl("lblEnlist"), Label).Text
            Else
                Return TryCast(Me.gv2.SelectedRow.FindControl("lblEnlist"), Label).Text
            End If

        End Get
    End Property

    Public ReadOnly Property VOrganizationID() As String
        Get
            If Me.gv2.SelectedRow Is Nothing Then
                Return TryCast(Me.gv2.Rows(0).FindControl("lblVOrganizationID"), Label).Text
            Else
                Return TryCast(Me.gv2.SelectedRow.FindControl("lblVOrganizationID"), Label).Text
            End If

        End Get
    End Property

    Public ReadOnly Property SelecteDataSource() As String
        Get
            Return Me.ddlDataSource.SelectedValue
        End Get
    End Property

    Public ReadOnly Property SelectedApplicationID() As String
        Get
            If Me.gv.SelectedRow Is Nothing Then
                Return Me.gv.DataKeys(0)("ApplicationID")
            Else
                Return Me.gv.DataKeys(Me.gv.SelectedRow.RowIndex)("ApplicationID")
            End If

        End Get
    End Property

    Public ReadOnly Property SelectedApplicationID2() As String
        Get
            If Me.gv.SelectedRow Is Nothing Then
                Return Me.gv2.DataKeys(0)("ApplicationID")
            Else
                Return Me.gv2.DataKeys(Me.gv2.SelectedRow.RowIndex)("ApplicationID")
            End If

        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return Me.gv.Rows.Count
        End Get
    End Property

    Public ReadOnly Property Count2() As Integer
        Get
            Return Me.gv2.Rows.Count
        End Get
    End Property

    Public ReadOnly Property SelectedStatus() As My.FormStatus
        Get
            Return New My.FormStatus(Me.ddlStatus.SelectedValue)
        End Get
    End Property

    Public ReadOnly Property VolunteerStatus() As My.VolunteerStatus
        Get
            Select Case New My.FormStatus(Me.ddlStatus.SelectedValue)
                Case My.FormStatus.Approved
                    Return My.VolunteerStatus.Approved
                Case My.FormStatus.Rejected
                    Return My.VolunteerStatus.Rejected
                Case My.FormStatus.Approving
                    Return My.VolunteerStatus.Approving
            End Select

            Return My.VolunteerStatus.Approving
        End Get
    End Property

    Public ReadOnly Property ApplicationDataSource() As My.VolunterApplicationDataSource
        Get
            Return New My.VolunterApplicationDataSource(Me.ddlDataSource.SelectedValue)
        End Get
    End Property

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
    Public Event DataBound(ByVal source As Object, ByVal e As EventArgs)
    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)

End Class